package com.yq.simpleindicator;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

/**
 * @功能描述：ViewPager适配器，用来绑定数据和view
 */
public class ViewPagerAdapter extends PagerAdapter {
    //界面列表
    private ArrayList<? extends View> views;

    public ViewPagerAdapter(ArrayList<? extends View> views) {

        this.views = views;

    }

    /**
     * 获得当前界面数
     */
    @Override
    public int getCount() {

        if (views != null) {

            return views.size();

        } else return 0;

    }

    /**
     * 判断是否由对象生成界面
     */
    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return (arg0 == arg1);
    }

    /**
     * 销毁position位置的界面
     */
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

        container.removeView(views.get(position));

    }

    /**
     * 初始化position位置的界面
     */
    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        container.addView(views.get(position), 0);

        return views.get(position);

    }

}
